package com.sdhs.paas.tcc.coordinator.example.service;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdhs.paas.tcc.coordinator.example.dto.OrderRequest;

@RestController
@RequestMapping(value="/tcc")
public class TccTestController {
	 @Autowired
	    TccOrderService tccOrderService;
	 	
	 	@GetMapping("/test1")
	    public String testTccOrder() {
	        String txId = UUID.randomUUID().toString();
	        OrderRequest orderRequest = OrderRequest.builder()
	                .userId("caibosi")
	                .productCode("spring-cloud-in-action")
	                .quantity(10)
	                .build();
	        tccOrderService.newOrderWithTcc(orderRequest,txId);
	        return "testTccOrder";
	    }
	 	
	 	@GetMapping("/test2")
	    public void testTccOrderRetry(){
	        String txId = "972c86d6-6bda-4859-9128-de10f4bf9634";
	        OrderRequest orderRequest = OrderRequest.builder()
	                .userId("caibosi")
	                .productCode("spring-cloud-in-action")
	                .quantity(10)
	                .build();
	        tccOrderService.newOrderWithTcc(orderRequest,txId);
	    }
	 	
	 	@GetMapping("/test3")
	    public void testTccOrderRollback(){
	        String txId = UUID.randomUUID().toString();
	        OrderRequest orderRequest = OrderRequest.builder()
	                .userId("caibosi")
	                .productCode("spring-cloud-in-action")
	                .quantity(999)
	                .build();
	        tccOrderService.newOrderWithTcc(orderRequest,txId);
	    }
}
